package com.bnz.bnzshop.controller;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.bnz.bnzshop.config.BosProperties;
import com.bnz.bnzshop.vo.Goods;
import com.bnz.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bnz.bnzshop.entity.GoodsEntity;
import com.bnz.bnzshop.service.GoodsService;
import com.bnz.common.utils.PageUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * 商品
 *
 * @author baizhi
 * @email 315104869@qq.com
 * @date 2022-10-31 22:51:05
 */
@RestController
    @RequestMapping("shop/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BosProperties bosProperties;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manager:goods:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") String id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody Goods goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody Goods goods){
		goodsService.updateAll(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody String[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/uploadPic")
        public R upload(MultipartFile file) throws IOException {
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(bosProperties.getAccessKeyId(),bosProperties.getSecretAccessKey()));
        config.setEndpoint(bosProperties.getEndPoint());
        BosClient client = new BosClient(config);
        InputStream input = file.getInputStream();

        String dtStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        String name =dtStr+"/"+ UUID.randomUUID().toString()+file.getOriginalFilename();
        client.putObject(bosProperties.getBucketname(),name,input);
        client.shutdown();
        System.out.println(file.getOriginalFilename());
        String url = "https://baizhi.gz.bcebos.com/"+name;
        return R.ok().put("url",url);
    }
    @RequestMapping("/one/{id}")
    public R gerOne(@PathVariable("id") Long id){
        Goods goods = goodsService.selectOne(id);
        return R.ok().put("dataForm",goods);
    }

}
